import 'package:flutter/material.dart';

import 'utils/toast.dart';

class WillPopScopeRoute extends StatefulWidget {
  @override
  _WillPopScopeState createState() => _WillPopScopeState();
}

class _WillPopScopeState extends State<WillPopScopeRoute> {
  DateTime _lassPressedAt;

  @override
  Widget build(BuildContext context) {
    // 拦截返回事件
    return WillPopScope(
      child: Container(
        alignment: Alignment.center,
        child: Text('页面展示'),
      ),
      onWillPop: () async {
        if (_lassPressedAt == null ||
            DateTime.now().difference(_lassPressedAt) > Duration(seconds: 1)) {
          _lassPressedAt = DateTime.now();
          Toast.toast(context, msg: '再次点击退出');
          return false;
        }
        return true;
      },
    );
  }
}
